package com.example.multi.data.provider.controller;

import com.example.multi.data.provider.model.data.Showapi_res_result;
import com.example.multi.data.provider.model.data.TodayStatictic;
import com.example.multi.data.provider.model.news.NewsList;
import com.example.multi.data.provider.model.vo.OptionVo;
import com.example.multi.data.provider.service.yiqing.IYiqingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/yiqing")
public class YiqingController {

    @Autowired
    private IYiqingService yiqingService;


    @RequestMapping(value = {""}, method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object query() {
        Showapi_res_result result = yiqingService.query();
        ResponseEntity<Object> response = ResponseEntity.ok().body(result);
        return response;
    }


    @RequestMapping(value = {"/province"}, method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object queryProvince() {
        Showapi_res_result result = yiqingService.queryProvince();
        ResponseEntity<Object> response = ResponseEntity.ok().body(result);
        return response;
    }


    @RequestMapping(value = {"/province-simple"}, method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object queryProvinceSimple() {
        List<OptionVo> result = yiqingService.queryProvinceSimple();


        String updateTime = result.get(0).getTime();


        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("result", result);

        resultMap.put("update_time", updateTime);
        Map<String, Object> valueMap = result.stream().collect(Collectors.toMap(OptionVo::getCode, OptionVo::getValue));
        Map<String, Object> value1Map = result.stream().collect(Collectors.toMap(OptionVo::getCode, OptionVo::getValue1));

        resultMap.put("value_map", valueMap);
        resultMap.put("value1_map", value1Map);
        ResponseEntity<Object> response = ResponseEntity.ok().body(resultMap);
        return response;
    }

    @RequestMapping(value = {"/today"}, method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object todayStatictic() {
        TodayStatictic result = yiqingService.queryTodayStatictic();
        ResponseEntity<Object> response = ResponseEntity.ok().body(result);
        return response;
    }

    @RequestMapping(value = {"/news"}, method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object news() {
        List<NewsList> result = yiqingService.queryNews();
        ResponseEntity<Object> response = ResponseEntity.ok().body(result);
        return response;
    }
}
